package com.example.quickleave;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class ImageSaver {

    public static final String FOLDER_NAME = "QuickLeave";

    public static boolean saveImageToGallery(Context context, Bitmap bitmap) {
        // 获取当前时间作为文件名
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
        // 文件名
        String fileName = "IMG_" + timeStamp + ".jpg";

        boolean success;

        // 首先尝试将图片保存到公共图库中
        success = saveImageToPublicGallery(context, bitmap, fileName);

        // 如果保存到公共图库失败，尝试保存到私有目录中
        if (!success) {
            success = saveImageToPrivateDirectory(context, bitmap, fileName);
        }

        return success;
    }

    private static boolean saveImageToPublicGallery(Context context, Bitmap bitmap, String fileName) {
        boolean success = false;

        // 设置图片保存的路径
        String relativeLocation = Environment.DIRECTORY_PICTURES + File.separator + FOLDER_NAME;

        // 创建保存图片的 ContentValues 对象
        ContentValues contentValues = new ContentValues();
        contentValues.put(MediaStore.Images.Media.DISPLAY_NAME, fileName);
        contentValues.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
        contentValues.put(MediaStore.Images.Media.RELATIVE_PATH, relativeLocation);

        // 获取 ContentResolver
        ContentResolver resolver = context.getContentResolver();

        // 向 MediaStore 插入图片
        Uri uri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);

        if (uri != null) {
            try {
                // 打开输出流
                OutputStream outputStream = resolver.openOutputStream(uri);

                if (outputStream != null) {
                    // 将 Bitmap 保存到输出流
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);

                    // 关闭输出流
                    outputStream.close();

                    success = true;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return success;
    }

    private static boolean saveImageToPrivateDirectory(Context context, Bitmap bitmap, String fileName) {
        boolean success = false;

        try {
            // 创建保存图片的目录
            File directory = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES), FOLDER_NAME);
            if (!directory.exists()) {
                directory.mkdirs();
            }

            // 创建图片文件
            File file = new File(directory, fileName);

            // 创建输出流
            FileOutputStream outputStream = new FileOutputStream(file);

            // 将 Bitmap 保存到输出流
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);

            // 关闭输出流
            outputStream.close();

            // 刷新媒体库，通知系统有新文件加入
            MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), file.getName(), null);

            success = true;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return success;
    }
}
